package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.Orders;
import com.ws.ssmzongjie3.bean.ProVo;
import com.ws.ssmzongjie3.bean.Route;
import com.ws.ssmzongjie3.bean.User;
import com.ws.ssmzongjie3.service.OrdersService;
import com.ws.ssmzongjie3.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    //订单
    @Autowired
    OrdersService ordersService;
    @Autowired
    RouteService routeService;

    @RequestMapping("/list")
    public String list(Model model, ProVo proVo, Orders orders, HttpSession session){

       System.out.println("links:"+orders);
       PageHelper pageHelper = new PageHelper();//分页帮助类
       pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Orders> orders1 = ordersService.listOrderWithRouteAndUser(orders);
        PageInfo pageInfo = new PageInfo(orders1);
       model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("orders1",orders1);
        model.addAttribute("orders",orders);
        return "/WEB-INF/admin/order/list";
    }

    @RequestMapping("/qianlist")
    public String select(Model model, ProVo proVo, Orders orders, HttpSession session){
        User user = (User) session.getAttribute("user");
        System.out.println("links:"+orders);
        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        orders.setUser_id(user.getUser_id());
        List<Orders> orders1 = ordersService.listOrderWithRouteAndUser(orders);
        PageInfo pageInfo = new PageInfo(orders1);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("orders",orders);

        return "/WEB-INF/qiantai/order/list";
    }

    @RequestMapping("/delete")
    public String delete(Orders orders) {
        ordersService.deleteOrderById(orders);
        return "redirect:/order/qianlist";
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> createOrder(@RequestBody Orders orders) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 设置默认值
            orders.setOrders_state(0); // 0表示待支付
            orders.setBuy_date(new java.util.Date()); // 设置当前时间为下单时间

            ordersService.insertOrder(orders);
            result.put("success", true);
            result.put("message", "订单提交成功！");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "订单提交失败：" + e.getMessage());
        }
        return result;
    }
}
